package com.ruoyi.web.controller.module.market;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.market.domain.ConfigMessage;
import com.ruoyi.market.service.IConfigMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 店铺消息配置 信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/market/configMessage")
public class ConfigMessageController extends BaseController
{
    @Autowired
    private IConfigMessageService configMessageService;

    /**
     * 获取店铺消息配置列表
     */
    @PostMapping("/configMessageList")
    public TableDataInfo list(@RequestBody ConfigMessage configMessage)
    {
        startPageByPost();
        List<ConfigMessage> list = configMessageService.selectConfigMessageList(configMessage);
        return getDataTable(list);
    }

    /**
     * 根据店铺消息配置编号获取详细信息
     */
    @PostMapping("/getConfigMessage")
    public AjaxResult getConfigMessage(@RequestBody Map<String, Object> params)
    {
        String shopId = params.get("shopId").toString(); // 店铺id
        if (shopId == null || shopId.equals("")) {
            return error("店铺消息配置失败，参数错误");
        }
        AjaxResult ajax = AjaxResult.success();
        ConfigMessage sysConfigMessage = configMessageService.selectConfigMessageByShopId(shopId);
        ajax.put(AjaxResult.DATA_TAG, sysConfigMessage);
        return ajax;
    }

    /**
     * 新增店铺消息配置
     */
    @PreAuthorize("@ss.hasPermi('market:configMessage:add')")
    @Log(title = "店铺消息配置管理 - 新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ConfigMessage configMessage)
    {
        configMessage.setCreateBy(getUsername());
        return toAjax(configMessageService.insertConfigMessage(configMessage));
    }

    /**
     * 修改店铺消息配置
     */
    @PreAuthorize("@ss.hasPermi('market:configMessage:edit')")
    @Log(title = "店铺消息配置管理 - 修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody ConfigMessage configMessage)
    {
        configMessage.setUpdateBy(getUsername());
        return toAjax(configMessageService.updateConfigMessage(configMessage));
    }

    /**
     * 保存店铺消息配置
     */
    @PostMapping("/save")
    public AjaxResult save(@Validated @RequestBody ConfigMessage configMessage)
    {
        if(StringUtils.isNotNull(configMessage) && StringUtils.isNotNull(configMessage.getShopId())){
            ConfigMessage cb = configMessageService.selectConfigMessageByShopId(configMessage.getShopId());
            if(StringUtils.isNotNull(cb)){
                configMessageService.updateConfigMessage(configMessage);
            }else{
                configMessageService.insertConfigMessage(configMessage);
            }
        }else {
            return error("保存店铺消息配置失败，参数错误");
        }
        configMessage.setCreateBy(getUsername());
        configMessage.setUpdateBy(getUsername());
        return toAjax(configMessageService.updateConfigMessage(configMessage));
    }

    /**
     * 删除店铺消息配置
     */
    @PreAuthorize("@ss.hasPermi('market:configMessage:remove')")
    @Log(title = "店铺消息配置管理 - 删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configMessageIds}")
    public AjaxResult remove(@PathVariable Long[] configMessageIds)
    {
        return toAjax(configMessageService.deleteConfigMessageByIds(configMessageIds));
    }
}
